package com.supermap.disaster.service.impl;


import com.supermap.agrinfo.entity.Agrinfo;
import com.supermap.common.JSONUtil;
import com.supermap.disaster.dao.DisasterDao;
import com.supermap.disaster.entity.Disaster;
import com.supermap.disaster.entity.DisasterComment;
import com.supermap.disaster.service.DisasterService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author:langhongshuai
 * @Description:农情业务逻辑层
 * @Date:18/1/29 下午3:01
 */
@Service
public class DisasterServiceImpl implements DisasterService {
    /**
     *使用指定类初始化日志对象，在日志输出的时候，可以打印出日志信息所在类
     */
    protected static final Logger logger = LoggerFactory.getLogger(DisasterServiceImpl.class);

    @Resource
    private DisasterDao disasterDao;

    @Override
    public JSONUtil getDisasterByUIdArea(String userId,String areaCode) {
        //初始化Json封装工具类
        JSONUtil jsonUtil = new JSONUtil();
        try {
            if(areaCode!=null){
                areaCode=areaCode+"%";
            }
            //获得用户所有的农情信息
            List<Disaster> disasters = disasterDao.getDisasterByUIdArea(userId,areaCode);

            //获得用户所有的农情信息后返回的json数据
            jsonUtil.setMsg("Get user disaster success！");
            jsonUtil.setIsSuccess(true);
            jsonUtil.setCode(200);
            jsonUtil.setData(disasters);
        }catch (Exception e) {
            //捕捉数据库异常
            logger.error("DisasterServiceImpl=>getDisasterByUserId=>DaoException:"+e.getStackTrace());
            jsonUtil.setMsg("Get user disaster flase！");
            jsonUtil.setIsSuccess(false);
            jsonUtil.setCode(100);
        }
        return jsonUtil;
    }

    @Override
    public JSONUtil addDisaster(Disaster disaster) {
        //初始化Json封装工具类
        JSONUtil jsonUtil = new JSONUtil();
        try {
            //获得用户所有的农情信息
           disasterDao.addDisaster(disaster);

            //获得用户所有的农情信息后返回的json数据
            jsonUtil.setMsg("add user disaster success！");
            jsonUtil.setIsSuccess(true);
            jsonUtil.setCode(200);
            jsonUtil.setData(disaster);
        }catch (Exception e) {
            //捕捉数据库异常
            logger.error("DisasterServiceImpl=>addDisaster=>DaoException:"+e.getStackTrace());
            jsonUtil.setMsg("add user disaster flase！");
            jsonUtil.setIsSuccess(false);
            jsonUtil.setCode(100);
        }
        return jsonUtil;
    }

    @Override
    public JSONUtil getAllDisaster() {
//初始化Json封装工具类
        JSONUtil jsonUtil = new JSONUtil();
        try {
            //获得用户所有的农情信息
             List<Disaster> disasterList = disasterDao.getAllDisaster();

            //获得用户所有的农情评论信息
             List<DisasterComment> disasterComments = disasterDao.getAllDisasterComment();

            for (Disaster disaster:disasterList) {
                List<DisasterComment> newdisasterComments = new ArrayList<>();
                for(DisasterComment disasterComment:disasterComments){
                    if(disaster.getId()==disasterComment.getDisasterId()){
                        newdisasterComments.add(disasterComment);
                    }
                }
                disaster.setDisasterComments(newdisasterComments);
            }
            //获得用户所有的农情信息后返回的json数据
            jsonUtil.setMsg("Get all disaster success！");
            jsonUtil.setIsSuccess(true);
            jsonUtil.setCode(200);
            jsonUtil.setData(disasterList);
        }catch (Exception e) {
            //捕捉数据库异常
            logger.error("DisasterServiceImpl=>getAllDisaster=>DaoException:"+e.getMessage());
            jsonUtil.setMsg("Get all disaster flase！");
            jsonUtil.setIsSuccess(false);
            jsonUtil.setCode(100);
        }
        return jsonUtil;
    }

    @Override
    public JSONUtil delDisaster(int disasterId) {
        //初始化Json封装工具类
        JSONUtil jsonUtil = new JSONUtil();
        try {
            //获得用户所有的农情信息
            disasterDao.delDisaster(disasterId);

            //获得用户所有的农情信息后返回的json数据
            jsonUtil.setMsg("del user disaster success！");
            jsonUtil.setIsSuccess(true);
            jsonUtil.setCode(200);
            jsonUtil.setData(disasterId);
        }catch (Exception e) {
            //捕捉数据库异常
            logger.error("DisasterServiceImpl=>delDisaster=>DaoException:"+e.getStackTrace());
            jsonUtil.setMsg("del user disaster flase！");
            jsonUtil.setIsSuccess(false);
            jsonUtil.setCode(100);
        }
        return jsonUtil;
    }

    @Override
    public JSONUtil getDisasterByareacode(String areacode) {
        //初始化Json封装工具类
        JSONUtil jsonUtil = new JSONUtil();
        try {
            List<Disaster> disasterList= disasterDao.getDisasterByareacode(areacode);
            //获得用户所有的农情信息后返回的json数据
            jsonUtil.setMsg("Get all disaster by areacod success！");
            jsonUtil.setIsSuccess(true);
            jsonUtil.setCode(200);
            jsonUtil.setData(disasterList);
        }catch (Exception e){
            logger.error("DisasterServiceImpl=>getDisasterByareacode=>DaoException:"+e.getMessage());
            jsonUtil.setMsg("Get all disaster  by areacode flase！");
            jsonUtil.setIsSuccess(false);
            jsonUtil.setCode(100);
        }
        return jsonUtil;
    }
    @Override
    public JSONUtil getDisasters(String type,String areaCode,String disasterType,String dateTime) {
        //初始化Json封装工具类
        JSONUtil jsonUtil = new JSONUtil();
        try {
            List<Disaster> agr = disasterDao.getDisasters(type,areaCode,disasterType,dateTime);
            jsonUtil.setMsg("getDisasterByCropId success！");
            jsonUtil.setIsSuccess(true);
            jsonUtil.setCode(200);
            jsonUtil.setData(agr);
        }catch (Exception e) {
            //捕捉数据库异常
            logger.error("DisasterServiceImpl=>getDisasterByCropId=>DaoException:"+e.getMessage());
            jsonUtil.setMsg("getDisasterByCropId flase！");
            jsonUtil.setIsSuccess(false);
            jsonUtil.setCode(100);
        }
        return jsonUtil;
    }

}
